#!/bin/bash

# Exit as soon as something errors.
set -e

if [[ ! "$_PYAV_ACTIVATED" ]]; then
    export here="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)"
    source "$here/activate.sh"
fi

cd "$PYAV_ROOT"


TESTSUITE="${1-main}"

istest() {
    [[ "$TESTSUITE" == all || "$TESTSUITE" == "$1" ]]
    return $?
}

if istest flake8; then
    # Settings are in setup.cfg
    $PYAV_PYTHON -m flake8 av examples tests
fi

if istest isort; then
    # More settings in setup.cfg
    $PYAV_PYTHON -m isort --check-only --diff av examples tests
fi

if istest main; then
    $PYAV_PYTHON setup.py test
fi

if istest sdist; then
    $PYAV_PYTHON setup.py build_ext
    $PYAV_PYTHON setup.py sdist
    if [[ "$TRAVIS_TAG" ]]; then
        $PYAV_PIP install twine
        $PYAV_PYTHON -m twine upload --skip-existing dist/*
    fi
fi

if istest doctest; then
    make -C docs test
fi

if istest examples; then
    for name in $(find examples -name '*.py'); do
        echo
        echo === $name
        cd "$PYAV_ROOT"
        mkdir -p "sandbox/$1"
        cd "sandbox/$1"
        if ! python "$PYAV_ROOT/$name"; then
            echo FAILED $name with code $?
            exit $?
        fi
    done
fi
